package com.cskaoyan.service.wxorder;

import com.cskaoyan.bean.user.WxUserLoginData;
import com.cskaoyan.bean.wxcart.HandleOptionBean;
import com.cskaoyan.bo.wxcart.WxCartDeleteBo;
import com.cskaoyan.bo.wxorder.*;
import com.cskaoyan.mapper.WxOrderMapper;
import com.cskaoyan.service.wxcart.WxCartService;
import com.cskaoyan.vo.wxorder.OrderCommentInfoVo;
import com.cskaoyan.vo.wxorder.OrderDetailVo;
import com.cskaoyan.vo.wxorder.OrderListVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class WxOrderServiceImpl implements WxOrderService {

    @Autowired
    WxOrderMapper wxOrderMapper;

    @Autowired
    WxCartService wxCartService;

    @Override
    public int submitOrder(OrderSubmitBo orderSubmitBo) {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // Integer userId = 1;

        OrderInsertBo orderInsertBo = new OrderInsertBo();

        // 1、找出购物车中所有被checked的商品
        List<OrderProductBo> orderProductBoList = wxOrderMapper.selectCheckedProduct(userId, orderSubmitBo.getCartId());

        // 判断库存
        for (OrderProductBo bo : orderProductBoList) {
            int productNum = wxOrderMapper.selectProductNum(bo.getProductId());
            if (productNum - bo.getNumber() < 0)
                return -1;
        }

        // 2、根据addressid找出consignee，mobile，address
        OrderAddressBo orderAddressBo = wxOrderMapper.selectAddressInfo(orderSubmitBo.getAddressId());
        orderInsertBo.setConsignee(orderAddressBo.getName());
        orderInsertBo.setAddress(orderAddressBo.getProviceName() + orderAddressBo.getCityName() + orderAddressBo.getAreaName() + orderAddressBo.getAddress());
        orderInsertBo.setMobile(orderAddressBo.getMobile());

        orderInsertBo.setUserId(userId);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderSo = sdf.format(new Date());
        orderInsertBo.setOrderSn(orderSo);
        orderInsertBo.setOrderState(101);
        orderInsertBo.setMessage(orderSubmitBo.getMessage());

        BigDecimal goodsPrice = BigDecimal.valueOf(0);

        for (OrderProductBo bo : orderProductBoList) {
            goodsPrice = goodsPrice.add(bo.getPrice());
        }
        orderInsertBo.setGoodsPrice(goodsPrice);
        orderInsertBo.setOrderPrice(goodsPrice);
        orderInsertBo.setActualPrice(goodsPrice);
        orderInsertBo.setComments(orderProductBoList.size());

        // 插入order表
        wxOrderMapper.insertOrder(orderInsertBo);

        // 插入order_product

        wxOrderMapper.insertOrderAndProduct(orderProductBoList, orderInsertBo.getId());

        // 删除购物车中的商品
        // wxOrderMapper.deletedCheckedProduct(userId);
        List<Integer> productIdList = new ArrayList<>();
        for (OrderProductBo productBo : orderProductBoList) {
            productIdList.add(productBo.getProductId());
        }

        wxCartService.cartDelete(new WxCartDeleteBo(productIdList));


        return orderInsertBo.getId();
    }

    @Override
    public OrderListVo listOrder(Integer showType, Integer page, Integer size) {

        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();

        OrderListVo.DataBean dataBean = new OrderListVo.DataBean();
        List<OrderListVo.DataBean> dataBeans = wxOrderMapper.selectOrder(showType, userId);

        for (OrderListVo.DataBean bean : dataBeans) {
            List<OrderListVo.DataBean.GoodsListBean> goodBeanList = wxOrderMapper.selectProductInfo(bean.getId());
            bean.setGoodsList(goodBeanList);
            HandleOptionBean handleOptionBean = getHandleOption(bean.getOrderStatusText());
            bean.setHandleOption(handleOptionBean);
        }


        OrderListVo orderListVo = new OrderListVo();

        orderListVo.setTotalPages(1);
        orderListVo.setCount(dataBeans.size());
        orderListVo.setData(dataBeans);
        return orderListVo;
    }


    private HandleOptionBean getHandleOption(String statusText) {
        HandleOptionBean handleOptionBean = new HandleOptionBean();
        if (statusText.equals("未付款")) {
            handleOptionBean.setCancel(true);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(true);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("待发货")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(true);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("待收货")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(true);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("待评价")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(true);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(true);
        } else if (statusText.equals("已取消(系统)")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("订单取消，退款中")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(false);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("已取消")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("已退款")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(false);
        } else if (statusText.equals("已收货(系统)")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(true);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(true);
        } else if (statusText.equals("已完成")) {
            handleOptionBean.setCancel(false);
            handleOptionBean.setDelete(true);
            handleOptionBean.setPay(false);
            handleOptionBean.setComment(false);
            handleOptionBean.setConfirm(false);
            handleOptionBean.setRefund(false);
            handleOptionBean.setRebuy(true);
        }


        return handleOptionBean;
    }

    @Override
    public OrderDetailVo selectOrderDetail(Integer orderId) {
        OrderDetailVo orderDetailVo = new OrderDetailVo();

        OrderDetailVo.OrderInfoBean orderInfoBean = wxOrderMapper.selectOrderInfo(orderId);
        List<OrderDetailVo.OrderGoodsBean> goodsBeanList = wxOrderMapper.selectGoodInfo(orderId);

        HandleOptionBean handleOptionBean = getHandleOption(orderInfoBean.getOrderStatusText());
        orderInfoBean.setHandleOption(handleOptionBean);

        orderDetailVo.setOrderInfo(orderInfoBean);
        orderDetailVo.setOrderGoods(goodsBeanList);
        return orderDetailVo;
    }

    @Override
    public int deleteOrder(Integer orderId) {
        wxOrderMapper.deleteOrder(orderId);
        wxOrderMapper.deleteOrderProduct(orderId);
        return 0;
    }

    @Override
    public int cancelOrder(Integer orderId) {
        // 将订单状态改为102
        wxOrderMapper.cancelOrder(orderId);

        // order_goods表：找出productid和number
        List<OrderCancelBo> orderCancelBoList=wxOrderMapper.selectOrderCancelInfo(orderId);

        // good_product表：增加库存
        for (OrderCancelBo bo : orderCancelBoList) {
            wxOrderMapper.updateProductStock(bo);
        }

        return 0;
    }

    @Override
    public int comment(CommentBo commentBo) {
        // 插入评论
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();

        commentBo.setUserId(userId);
        wxOrderMapper.insertComment(commentBo);

        // 将order_goods表的comment设置为评论的id
        wxOrderMapper.updateOrderGoodComment(commentBo.getOrderGoodsId(), commentBo.getId());

        // order_product表：找到对应的订单id
        int orderId = wxOrderMapper.selectOrderId(commentBo.getOrderGoodsId());

        // 查找order表中的comment数量
        int commentNum = wxOrderMapper.selectCommentNum(orderId);

        // 如果-1之后，comment=0，设置订单状态为501：已完成
        wxOrderMapper.updateOrderStatus(orderId, (commentNum - 1));


        return 0;
    }

    @Override
    public int refund(Integer orderId) {
        // 修改订单状态为202
        wxOrderMapper.updateOrderRefund(orderId);
        return 0;
    }

    @Override
    public int confirmOrder(Integer orderId) {
        // 修改订单状态为401
        wxOrderMapper.updateOrderConfirm(orderId);
        return 0;
    }

    @Override
    public OrderCommentInfoVo goodCommentInfo(Integer orderId, Integer goodsId) {
        OrderCommentInfoVo orderCommentInfoVo = wxOrderMapper.selectGoodCommentInfo(orderId, goodsId);
        return orderCommentInfoVo;
    }


}
